export class GameController {
  cellArr: number[][] = []

  startGame() {
    this.cellArr = [
      [0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 0],
      [0, 0, 0, 0]
    ];
    this.fillOneEmptyCell()
    this.fillOneEmptyCell()
  }

  fillOneEmptyCell() {
    const emptyCells = this.findEmptyCells()
    const index = this.random(emptyCells.length)
    const [row, col] = emptyCells[index]
    this.cellArr[row][col] = this.getRandomValue()
  }

  findEmptyCells(): [number, number][] {
    const result: [number, number][] = []
    for (let i = 0; i < 4; i++) {
      for (let j = 0; j < 4; j++) {
        if (this.cellArr[i][j] === 0) {
          result.push([i, j])
        }
      }
    }
    return result
  }

  getRandomValue() {
    if (Math.random() < 0.9) {
      return 2
    } else {
      return 4
    }
  }

  random(max: number) {
    return Math.floor(Math.random() * max)
  }

  getFlatCellArr() {
    return this.cellArr.flat()
  }
}